<?php

    class Breadcrumbs
    {
        /**
         * Generates breadcrumbs for page.
         *
         * @param string $pageId
         * @param array of PathInfo Objects $subBreadcrumpsChain
         * @return string
         */
        public static function Generate( $pageId, $subBreadcrumbsChain = array() )
        {
            $result     = "";
            $pathInfo   = array();
            
            $pathInfo = ( empty( $subBreadcrumbsChain ) ) ? self::PrepareChainToPage( $pageId ) : $subBreadcrumbsChain;
    
            return self::GenerateByPathInfo( $pathInfo );
        }

        public static function GenerateWrapped($pageId, $subBreadcrumbsChain = array())
        {
            $unwrapped = self::Generate($pageId, $subBreadcrumbsChain);

            $tmpl = Core::CreateTemplateEngine();
            return $tmpl->getView(PATH_TEMPLATES . 'include/breadcrumbs.tpl', array(
                'BREADCRUMBS' => $unwrapped
            ));
        }
        
        /**
         * Prepare chain to page. Generates array of PageInfo objects.
         *
         * @param string $pageId
         * @return array
         */
        public static function PrepareChainToPage( $pageId )
        {
            $pages = Pages::GetChainToPage( $pageId );
            foreach ( $pages as $pageId )
            {
                $pageInfo = Pages::GetPageInfo( $pageId );
                if ( !$pageInfo->IsTranslatedPage() )
                {
                    $translatedPageId = PageInfo::GetTranslatedPageId( $pageInfo->id, Core::GetLanguage() );
                    $translatedPageInfo = Pages::GetPageInfo( $translatedPageId );
                    if ( !is_null( $translatedPageInfo ) )
                    {
                        $pageInfo = $translatedPageInfo;
                    }
                }
                $pathInfo[] = $pageInfo;
            }
            
            return $pathInfo;  
        }

        public static function AddPageToChain( $pageId, $lastPageName )
        {
            $chain = Breadcrumbs::PrepareChainToPage( $pageId );

            $pageInfo = new PageInfo();
            $pageInfo->name = $lastPageName;
            $chain[] = $pageInfo;

            return $chain;
        }
        
        /**
         * Generates breadcrumps view by $pathInfo
         *
         * @param array of PathInfo Objects $pathInfo
         * @return string
         */
        private static function GenerateByPathInfo( $pathInfo )
        {
            $result = "";
            $tmpl   = Core::CreateTemplateEngine( PATH_TEMPLATES."include/breadcrumbs/" );
            foreach ( $pathInfo as $pageIndex => $pageInfo )
            {
                $tmpl->clearAll();
                $tmpl->define('link',    "breadcrumbs.tpl");
                $tmpl->define('no_link', "breadcrumbs_no_link.tpl");
                $tmpl->define('end',     "breadcrumbs_end.tpl");
    
                $tmpl->set("TEXT", $pageInfo->name);

                if ( $pageIndex == count($pathInfo) - 1 )
                {
                    $tmpl->parse("breadcrumbs", ".end");
                }
                else
                {
                    if ( $pageInfo->link )
                    {
                        if ( $pageInfo->path == "")
                        {
                            $tmpl->set("LINK", UrlHelper::GetAbsolutePath(SITE_URL));
                        }
                        else
                        {
                            $tmpl->set("LINK", UrlHelper::GetAbsolutePath($pageInfo->path));
                        }
    
                        $tmpl->parse("breadcrumbs", ".link");
                    }
                    else
                    {
                        $tmpl->parse("breadcrumbs", ".no_link");
                    }
                }
                $result .= $tmpl->getGenerated();
            }
            return $result;
        }
    }